package com.example.cwfdemo.utils;

import org.springframework.web.context.request.RequestContextHolder;
import org.springframework.web.context.request.ServletRequestAttributes;

import javax.servlet.http.HttpServletRequest;


public class HttpRequestUtils {

    /**
     * @param key
     * @param value
     */
    public static void setAttribute(String key, Object value) {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attrs.getRequest();
        request.setAttribute(key, value);
    }

    /**
     * 获取request对象
     *
     * @return
     */
    public static HttpServletRequest getRequest() {
        ServletRequestAttributes attrs = (ServletRequestAttributes) RequestContextHolder.getRequestAttributes();
        HttpServletRequest request = attrs.getRequest();
        return request;
    }


    /**
     * 获取参数
     *
     * @param name
     * @return String
     */
    public String getParameter(String name) {
        // 返回值之前 先进行过滤
        return getRequest().getParameter(XssShieldUtils.stripXss(name));
    }

    /**
     * 获取参数
     *
     * @param name
     * @return String[]
     */
    public String[] getParameterValues(String name) {
        // 返回值之前 先进行过滤
        String[] values = getRequest().getParameterValues(XssShieldUtils.stripXss(name));
        if (values != null) {
            for (int i = 0; i < values.length; i++) {
                values[i] = XssShieldUtils.stripXss(values[i]);
            }
        }
        return values;
    }


    /**
     * 获取ip
     *
     * @param request
     * @return
     */
    public static String getIp(HttpServletRequest request) {
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 获取ip
     *
     * @return
     */
    public static String getIp() {
        HttpServletRequest request = getRequest();
        String ip = request.getHeader("x-forwarded-for");
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getHeader("WL-Proxy-Client-IP");
        }
        if (ip == null || ip.length() == 0 || "unknown".equalsIgnoreCase(ip)) {
            ip = request.getRemoteAddr();
        }
        return ip;
    }

    /**
     * 获取域名
     *
     * @return
     * @date 2017-1-18
     */
    public static String getDomain() {
        HttpServletRequest request = getRequest();
        String domain = request.getServerName();
        return domain;
    }

}
